﻿
--[[    
    Paranoia Enemy Player Alert
    Copyright (C) 2008 Victor Barrancos
  
    This file is part of Paranoia Enemy Player Alert.

    Paranoia Enemy Player Alert is free software: you can redistribute it 
    and/or modify it under the terms of the GNU General Public License as
    published by the Free Software Foundation, either version 3 of the 
    License, or (at your option) any later version.

    Paranoia Enemy Player Alert is distributed in the hope that it will 
    be useful, but WITHOUT ANY WARRANTY; without even the implied warranty 
    of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
    General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Paranoia Enemy Player Alert.  If not, see 
    <http://www.gnu.org/licenses/>.
    
This file is the code for the database browser frame, it deals with searching, sorting, display, flagging, and some
other stuff all related to making the enemy database accessible to the user.

FauxScrollFrames are the BIGGEST PAIN IN THE ASS. i can has .NET listveiw control?
]]--

ParanoiaBrowser = {};

--declarations
local MAX_LINE_COUNT = 16;
local dbSearchResults = {};
local dbSearchIndex = {};


--events
function ParanoiaBrowser_OnListItemClick(self, button)
  if button == "LeftButton" then
    local listItemID = self:GetID()
    local listItemName = dbSearchIndex[listItemID + FauxScrollFrame_GetOffset(ParanoiaBrowserScrollFrame)];
    local player = Paranoia_edb.players[PARANOIA_REALMNAME][listItemName];
    if not player then return; end
  end
end

function ParanoiaBrowser_Update()
  ParanoiaBrowser_HidePlayerTooltip()
  local exists, name, level, class, guild, lastseen, kills, deaths, kos;
  local numResults = getn(dbSearchIndex);
  local button, buttonText;
	local listOffset = FauxScrollFrame_GetOffset(ParanoiaBrowserScrollFrame);
	local listIndex;
	local showScrollBar = false;
	if ( numResults > MAX_LINE_COUNT ) then
		showScrollBar = true;
	end
	ParanoiaBrowserNumResults:SetText(string.format(WHO_NUM_RESULTS, numResults));
  for i=1, MAX_LINE_COUNT, 1 do
    listIndex = listOffset + i;
    button = getglobal("ParanoiaBrowserButton"..i);
    button.listIndex = listIndex
    exists, name, level, class, guild, lastseen, kills, deaths, kos = ParanoiaBrowser_GetSearchResultData(listIndex);
    if listIndex <= numResults then
      if exists then
        buttonText = getglobal("ParanoiaBrowserButton"..i.."Name");
        buttonText:SetText(name);
        buttonText = getglobal("ParanoiaBrowserButton"..i.."Class");
        local r,g,b = Paranoia:ClassColor(class);
        buttonText:SetText(class);
        buttonText:SetTextColor(r,g,b);
        buttonText = getglobal("ParanoiaBrowserButton"..i.."Level");
        buttonText:SetText(ParanoiaBrowser_LevelToString(level));
        buttonText = getglobal("ParanoiaBrowserButton"..i.."Guild");
        if not guild then guild = NONE; end
        buttonText:SetText(guild);
        buttonText = getglobal("ParanoiaBrowserButton"..i.."LastSeen");
        buttonText:SetText(Paranoia:TimeToString(time()-lastseen, true));
        buttonText = getglobal("ParanoiaBrowserButton"..i.."KillDeath");
        buttonText:SetText(tostring(kills).."/"..tostring(deaths));
        buttonText = getglobal("ParanoiaBrowserButton"..i.."KoS");
        if kos == true then buttonText:SetText(YES); else buttonText:SetText(NO); end
        button:Show()
      end
    else
      button:Hide()
		end
		-- Highlight the correct who
		if ParanoiaBrowserScrollFrame.selectedPlayer == listIndex then
			button:LockHighlight();
		else
			button:UnlockHighlight();
		end
		
	end

	if not ParanoiaBrowserScrollFrame.selectedIndex then
--~ enable/disable kos buttons here
	else
		ParanoiaBrowserScrollFrame.selectedPlayer = ParanoiaBrowser_GetSearchResultData(ParanoiaBrowserScrollFrame.selectedIndex); 
	end
	FauxScrollFrame_Update(ParanoiaBrowserScrollFrame, numResults, MAX_LINE_COUNT, 16);
end


--search
--searches the database by player name and adds matches to the dbSearchResults table.
function ParanoiaBrowser_SearchDatabase(searchParams)
  dbSearchResults = {};
  if searchParams == nil then searchParams = ""; end
  for playerName, _ in pairs(Paranoia_edb.players[PARANOIA_REALMNAME][PARANOIA_OPPFACTIONENG]) do
    local isMatch = false;
    if searchParams == "" then
      isMatch = true;
    else
      if string.find(" "..playerName.." ", searchParams) then isMatch = true; end
    end
    if isMatch then
      dbSearchResults[playerName] = Paranoia_edb.players[PARANOIA_REALMNAME][PARANOIA_OPPFACTIONENG][playerName];
    end
  end
  ParanoiaBrowser_GenerateSearchIndex();
  ParanoiaBrowser_Update();
end

--assigns each search result a number, or index, so we can loop through them and display a certain range in the scrollframe.
function ParanoiaBrowser_GenerateSearchIndex()
  dbSearchIndex = {};
  local i = 0;
	for resultName, _ in pairs(dbSearchResults) do
    dbSearchIndex[i] = resultName;
    i = i + 1;
  end
end

--returns name, level, class, guild, lastseen, kills, deaths, and kos flag data for the given search result (by name or index)
function ParanoiaBrowser_GetSearchResultData(result)
  local resultName, pName, pLevel, pClass, pGuild, pLastSeen, pKills, pDeaths, pKos;
  if dbSearchResults == nil then ParanoiaBrowser_SearchDatabase(); end
  if type(result) == "number" then resultName = dbSearchIndex[result]; else resultName = result; end
  if not resultName then return false; end
  pName = resultName;
  pLevel = dbSearchResults[resultName][1]
  pClass = dbSearchResults[resultName][2];
  pGuild = dbSearchResults[resultName][3];
  pLastSeen = dbSearchResults[resultName][4];
  pKos = dbSearchResults[resultName][5];
  pKills = dbSearchResults[resultName][6];
  pDeaths = dbSearchResults[resultName][7];
  return true, pName, pLevel, pClass, pGuild, pLastSeen, pKills, pDeaths, pKos;
end


--tooltip
function ParanoiaBrowser_ShowPlayerTooltip(self)
  local listItemID = self:GetID()
  local listItemName = dbSearchIndex[listItemID + FauxScrollFrame_GetOffset(ParanoiaBrowserScrollFrame)];
  local player = Paranoia_edb.players[PARANOIA_REALMNAME][PARANOIA_OPPFACTIONENG][listItemName];
  local playerNameColorCode = "|cFFFFBBBB";
  if player[5] == true then playerNameColorCode = "|cFFFF0000"; end
  GameTooltip:SetOwner(self, "ANCHOR_CURSOR");
  GameTooltip:SetText(playerNameColorCode..listItemName.."|r\n");
  GameTooltip:Show();
end

function ParanoiaBrowser_HidePlayerTooltip()
  GameTooltip:Hide();
end


--helper
function ParanoiaBrowser_LevelToString(level) --Converts a numerical level from database to a string level.
  if level == 80 then
    return "80";
  elseif level == 0 then
    return "-";
  elseif level and level ~= 0 then
    return tostring(level).."+";
  else
    return "-";
  end
end

